package cn.lblbc.cootext

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.Switch
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar

class SettingsActivity : AppCompatActivity() {
    private lateinit var wrapTextSwitch: Switch
    private lateinit var settingsManager: SettingsManager
    private lateinit var aiProgrammingLayout: LinearLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)

        settingsManager = SettingsManager(this)

        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.title = "设置"

        wrapTextSwitch = findViewById(R.id.wrapTextSwitch)
        aiProgrammingLayout = findViewById(R.id.aiProgrammingLayout)

        // 初始化开关状态
        wrapTextSwitch.isChecked = settingsManager.isTextWrappingEnabled()

        // 设置开关变化监听器
        wrapTextSwitch.setOnCheckedChangeListener { _, isChecked ->
            settingsManager.setTextWrappingEnabled(isChecked)
        }

        setupListeners()
    }

    private fun setupListeners() {
        aiProgrammingLayout.setOnClickListener {
            val url = "https://cxyxy.blog.csdn.net/article/details/151590489"
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
            startActivity(intent)
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        finish()
        return true
    }
}

class SettingsManager(context: Context) {
    private val sharedPreferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE)

    fun isTextWrappingEnabled(): Boolean {
        return sharedPreferences.getBoolean("text_wrapping_enabled", false)
    }

    fun setTextWrappingEnabled(enabled: Boolean) {
        sharedPreferences.edit().putBoolean("text_wrapping_enabled", enabled).apply()
    }
}